package cn.est.controller;
import cn.est.pojo.Menus;
import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.dto.Result;
import cn.est.service.MenusService;
import cn.est.utils.EmptyUtils;
import cn.est.utils.ResultUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping("/client/menus")
public class MenusController {

    @Resource
    private MenusService menusService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<Menus> searchMenus(@RequestParam Map<String,Object> param) {
        Page<Menus> menusPage = null;
        try {
            Integer pageNo= EmptyUtils.isEmpty(param.get("pageNo"))? Page.DEFAULT_PAGE_NO:Integer.parseInt((String) param.get("pageNo"));
            Integer pageSize= EmptyUtils.isEmpty(param.get("pageSize"))? Page.DEFAULT_PAGE_SIZE:Integer.parseInt((String) param.get("pageSize"));
            menusPage  = menusService.queryMenusPageByMap(param, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menusPage;
    }

    @RequestMapping("/saveOrUpdateMenus")
    @ResponseBody
    public Result saveOrUpdateMenus(Menus menus){
        Integer flag=0;
        try {
            if(EmptyUtils.isEmpty(menus.getId())){
                flag=menusService.qdtxAddMenus(menus);
            }else{
                flag=menusService.qdtxModifyMenus(menus);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag>=1?ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

    @RequestMapping("/getMenusById")
    @ResponseBody
    public Result getMenusById(Long id){
        Result result= ResultUtils.returnSuccess();
        Menus menus=null;
        try {
            menus=menusService.getMenusById(id);
            result.setData(menus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/batchRemoveMenus")
    @ResponseBody
    public Result batchRemoveMenus(String ids){
        Integer flag=0;
        try {
            flag=menusService.qdtxBatchDeleteMenus(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/removeMenusById")
    @ResponseBody
    public Result removeMenusById(Long id){
        Integer flag=0;
        try {
            flag=menusService.qdtxDeleteMenusById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }
}
